package spring.autowire.context;

import spring.autowire.util.UtilConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootApplication
@Slf4j
public class AssembleDemoApplication implements ApplicationRunner {

	public static void main(String[] args) {
		SpringApplication.run(AssembleDemoApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {

		getBeanByApplication();
		log.info("=============");
		getBeanByXML();

	}
	private void getBeanByApplication(){
		ApplicationContext utilContext = new AnnotationConfigApplicationContext(UtilConfig.class);

		PrintText text = utilContext.getBean("creatBeanA", PrintText.class);
		text.hello();
	}

	private void getBeanByXML(){
		ApplicationContext utilContext = new AnnotationConfigApplicationContext(UtilConfig.class);
		ClassPathXmlApplicationContext barContext = new ClassPathXmlApplicationContext(
				new String[] {"applicationContext.xml"}, utilContext);
		PrintText text;

		text = barContext.getBean("creatBeanC", PrintText.class);
		text.hello();

		text = barContext.getBean("creatBeanB", PrintText.class);
		text.hello();
	}

}
